package com.pek.util;

public class RestResult<T> {
    /**
     * 状态码
     */
    private int code;

    /**
     * 是否请求成功
     */
    private boolean success;

    /**
     * 消息
     */
    private String message;

    /**
     * 返回值
     */
    private T Result;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return Result;
    }

    public void setResult(T result) {
        Result = result;
    }

    public RestResult() {
    }

    public RestResult(int code, boolean success, String message, T result) {
        this.code = code;
        this.success = success;
        this.message = message;
        Result = result;
    }

    /**
     * 成功
     * @param resObject 返回值
     */
    public static <V> RestResult<V> success(V resObject){
       RestResult<V> result=new RestResult<V>();
        result.setCode(200);
        result.setResult(resObject);
        result.setSuccess(true);
        result.setMessage("");
        return result;
    }

    /**
     * 成功 不返回值
     * @param message 返回消息
     */
    public static RestResult success(String message){
        RestResult<String> result=new RestResult<String>();
        result.setCode(200);
        result.setResult(message);
        result.setSuccess(true);
        result.setMessage(message);
        return result;
    }

    /**
     * 成功
     * @param message 返回消息
     * @param resObject 返回值
     */
    public static <V> RestResult<V> success(String message,V resObject){
        RestResult<V> result=new RestResult<V>();
        result.setCode(200);
        result.setResult(resObject);
        result.setSuccess(true);
        result.setMessage(message);
        return result;
    }

    /**
     * 成功 不返回值
     * @param message 返回消息
     */
    public static RestResult error(String message){
        RestResult result=new RestResult<String>();
        result.setCode(400);
        result.setResult(null);
        result.setSuccess(false);
        result.setMessage(message);
        return result;
    }

    /**
     * 失败
     * @param resObject 返回值
     */
    public static <V> RestResult<V> error(V resObject){
        RestResult<V> result=new RestResult<V>();
        result.setCode(400);
        result.setResult(resObject);
        result.setSuccess(false);
        result.setMessage("");
        return result;
    }


    /**
     * 失败
     * @param message 返回消息
     * @param resObject 返回值
     */
    public static <V> RestResult<V> error(String message,V resObject){
        RestResult<V> result=new RestResult<V>();
        result.setCode(400);
        result.setResult(resObject);
        result.setSuccess(false);
        result.setMessage(message);
        return result;
    }
}
